﻿using System;
using System.Windows;
using System.IO;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Graphics;

namespace ThreeDWorld
{
    public class Texture2DReader : ContentTypeReader<Texture2D>
    {
        public override Texture2D Read(ContentReader reader)
        {
            BitmapImage image = new BitmapImage();
            Texture2D texture;
            MemoryStream stream = null;

            try
            {
                image.SetSource(stream = new MemoryStream(reader.ReadByteArray()));
                texture = new Texture2D(reader.Content.Game.GraphicsDevice, image.PixelWidth, image.PixelHeight, false, SurfaceFormat.Color);
                image.CopyTo(texture);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
            return texture;
        }
    }
}
